package com.rover022.flixelgame.element
{
	import com.greensock.TimelineMax;
	import com.greensock.TweenLite;
	import com.rover022.flixelgame.GameConfig;
	import com.rover022.flixelgame.component.BaseComponent;
	import com.rover022.flixelgame.element.effect.NumEffect;
	import com.rover022.flixelgame.element.weapon.BaseFlxWeapon;
	import com.rover022.flixelgame.world.displayobject.FlxLoadImage;
	
	import flash.text.TextFieldAutoSize;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxPoint;
	import org.flixel.FlxText;
	import org.flixel.plugin.photonstorm.FlxBar;

	/**
	 * 元素  游戏一切活的物体的基类,其他的物体应该是他的子类
	 * 目前把这个类设计 为一个FlxGroup 为了方便在里面使用如[血条][名字]等会和他一起移动的FlxSprite
	 * @author flashpig
	 */
	public class BaseElement extends FlxGroup
	{
		
		/**
		 * 玩家状态
		 */
		public var state:String = PLAYSTATE.Stand;
		/**
		 * 动画线
		 */
		public var timelineMax:TimelineMax;
		/**
		 * 角色的身体
		 */
		public var bodyElement:FlxLoadImage;
		/**
		 * 角色的血条
		 */
		public var healthBar:FlxBar;
		/**
		 * 姓名条..以后还可以加类称号条
		 */
		public var nameBar:FlxText;
		/**
		 * 角色的组件集合
		 */
		public var components:Vector.<BaseComponent>;
		/**
		 * 角色阵营
		 */
		public var factionType:uint = 0;
		/**
		 * 攻击距离
		 */
		public var attackDistance:Number;
		/**
		 * 角色速度
		 */
		public var speed:Number = 1;
		/**
		 * 攻击间隔 毫秒为单位
		 */
		public var attackCDTime:int = 1000;
		/**
		 * 当前技能值
		 */
		public var skillPower:int = 0;
		/**
		 * 最大技能值
		 */
		public var maxSkillPower:int = 100;
		/**
		 * 角色的武器
		 */
		public var weapon:BaseFlxWeapon;
		/**
		 * 攻速基本常量 默认是1;
		 */
		public var baseSpeedRaid:Number = 1;
		//一些游戏中常用的设置 这些值都可以在初始化函数里面着设置
		public var z:int=0;
		public var ack:int = 1;
		public var maxHp:int = 1;
		public var hp:int = 1;
		public var mana:int = 0;
		public var exp:int = 0;
		public var deathExp:int = 20;
		public var lv:int = 1;
		public var maxLv:int = 3;
		public var width:Number = 104;
		public var height:Number = 104;
		public var name:String="英雄"
		public var imgId:String = "assets/UI/DOTA-HERO/Archer.jpg";
		
		private var _direction:int; //方向
		private var _targetB:BaseElement;
		

		public function BaseElement(MaxSize:uint = 0, vars:Object = null)
		{
			super(MaxSize);
			initProps(vars);
			initElement();
		}
	
		/**
		 * 设置属性 
		 * @param vars
		 * 
		 */		
		public function initProps(vars:*):void
		{
			if (vars != null)
			{
				for (var p:String in vars)
				{
					if (p in this)
					{
						this[p] = vars[p];
						 
					}
				}
			}
			
		}

		public function get direction():int
		{
			return _direction;
		}

		public function get isBusy():Boolean
		{
			if (state == PLAYSTATE.Walk || state == PLAYSTATE.Stand)
			{
				return false;
			}
			else
			{
				return true;
			}
		}

		/**
		 * 攻击速度 公式
		 * @return
		 *
		 */
		public function get attackSpeed():Number
		{
			return baseSpeedRaid * 1.4;
		}

		public function set direction(value:int):void
		{
			_direction = value;
			bodyElement.velocity.x = _direction * speed;
		}

		/**
		 * 初始化元素
		 */
		public function initElement():void
		{
			bodyElement = new FlxLoadImage();
			bodyElement.setImageUrl(GameConfig.webroot + imgId);
			bodyElement.offset.x=width/2;
			bodyElement.offset.y=height;
			healthBar = new FlxBar(0, 0, FlxBar.FILL_LEFT_TO_RIGHT, width, 10, bodyElement, "health");
			nameBar = new FlxText(0, 0, width, name , false);
			nameBar.size = 12;
			nameBar.alignment = TextFieldAutoSize.CENTER;
			nameBar.color = 0xffffff;
			add(bodyElement);
			add(healthBar);
			add(nameBar);
			components = new Vector.<BaseComponent>;
		}

		public function addComponent(src:BaseComponent):void
		{
			components.push(src);
		}

		override public function update():void
		{
			//组件逻辑跑完 设置完了速度矢量后自己再updtae;
			for (var i:int = 0; i < components.length; i++)
			{
				components[i].update();
			}
			super.update();
			//其他元件的位置要考虑body的 width 和 height
			nameBar.x = bodyElement.x - width/2;
			nameBar.y = bodyElement.y - height - 40;
			healthBar.x = bodyElement.x - width/2;
			healthBar.y = bodyElement.y -height - 20;
		}

		override public function destroy():void
		{
			for (var i:int = 0; i < components.length; i++)
			{
				components[i].destroy();
			}
			super.destroy();
		}

		/**
		 * 攻击
		 *
		 */
		public function attack():void
		{
			if (_targetB)
			{
				if (weapon)
				{
					weapon.fireAtElement(_targetB);
				}
				else
				{
					trace("我没有武器");
				}
			}
		}

		/**
		 * 攻击
		 *
		 */
		public function death():void
		{
		}

		/**
		 * 来自谁的攻击
		 * @param opponent
		 *
		 */
		public function hurtFrom(opponent:BaseElement):void
		{
			hurt(opponent.ack);
			if (hp == 0)
			{
				opponent.addExp(deathExp);
			}
		}

		/**
		 * 受伤.....
		 *
		 */
		public function hurt(num:Number):void
		{
			new NumEffect(num, bodyElement.x, bodyElement.y);
			if (hp > num)
			{
				swapState(PLAYSTATE.Hurt);
				hp -= num;
			}
			else
			{
				swapState(PLAYSTATE.Death);
				hp = 0;
			}
		}

		/**
		 * 瞄准
		 */
		public function sightTo(_pponent:BaseElement):void
		{
			//目前都是普通攻击 大技能 后期改造....
			_targetB = _pponent;
			swapState(PLAYSTATE.Attack);
		}

		/**
		 * 设置位置
		 * @param param0
		 * @param param1
		 *
		 */
		public function setPosition(_x:Number, _y:Number):void
		{
			bodyElement.x=_x;
			bodyElement.y=_y;
		}

		/**
		 * 添加时间轴动画 加上 FlxG.timeScale 与全局时间统一;
		 */
		public function addAnimation():void
		{
			timelineMax.add(new TweenLite(bodyElement, FlxG.timeScale * 2, {rotation: "360"}), PLAYSTATE.Stand); //站立
			timelineMax.add(new TweenLite(bodyElement, FlxG.timeScale * 2, {rotation: "360"}), PLAYSTATE.Walk); //行走
			timelineMax.add(new TweenLite(bodyElement, FlxG.timeScale * attackSpeed, {rotation: "360", onComplete: attackHadnle}), PLAYSTATE.Attack);
			timelineMax.add(new TweenLite(bodyElement, FlxG.timeScale * 2, {rotation: "360", onComplete: deathHadnle}), PLAYSTATE.Death);
			timelineMax.add(new TweenLite(bodyElement, FlxG.timeScale * 2, {rotation: "360", onComplete: hurtHadnle}), PLAYSTATE.Hurt);
			timelineMax.add(new TweenLite(bodyElement, FlxG.timeScale * 2, {rotation: "360", onComplete: reezeHadnle}), PLAYSTATE.Freeze);
			function deathHadnle():void
			{
				death();
			}
			function hurtHadnle():void
			{
				swapState(PLAYSTATE.Stand);
			}
			function attackHadnle():void
			{
				attack();
				swapState(PLAYSTATE.Stand);
			}
			function reezeHadnle():void
			{
				swapState(PLAYSTATE.Stand); //被打断之后 回到初始状态去
			}
		}

		/**
		 * 进入那种状态..
		 * @param Attack
		 *
		 */
		public function swapState(_state:String):void
		{
			if (_state != state)
			{
				state = _state;
				timelineMax.play(_state);
			}
		}

		/**
		 * 计算角色的坐标点位
		 * @return
		 *
		 */
		public function getMidpoint():FlxPoint
		{
			return bodyElement.getMidpoint();
		}

		/**
		 * 增加经验值
		 * @param deathExp
		 *
		 */
		public function addExp(deathExp:int):void
		{
			exp += deathExp;
		}
		
		public function get y():Number{
			return bodyElement.y;
		}
	}
}
